package com.jxs.android.sxyw;

import java.io.IOException;

import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.xmlpull.v1.XmlPullParserException;

import android.app.ProgressDialog;

import com.jxs.android.soap.BuWebService;
import com.jxs.android.soap.IWebService;
import com.jxs.android.sxyw.boot.SelfApplication;
import com.jxs.android.sxyw.data.OrderType;

public abstract class AbstractListActivity extends AbstractActivity {

	enum Which {
		PAGE, COUNT, CASE_TASK, CASE_ALARM, EOMS_ALARM, EOMS_POWER;
	}

	protected static final Integer PAGE_SIZE = 200;

	/**
	 * @Des 用于启动对应的应用程序
	 * @param showDialog
	 *            TODO
	 * @param context
	 * @param targetActivity
	 */
	protected void getWebServiceData(final String ticket, final int pageNo, final Which which, final boolean showDialog, final DataInitListeren dataInitListeren) {

		final ProgressDialog progressDialog = ProgressDialog.show(this, "", "正在加载，请稍候...", true);
		progressDialog.setCancelable(true);
		if (!showDialog) {
			progressDialog.cancel();
		}
		new Thread() {
			public void run() {
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				getListData(ticket, pageNo, which);
				dataInitListeren.initData(progressDialog, showDialog);
			}
		}.start();
	}

	static public interface DataInitListeren {
		public void initData(ProgressDialog progressDialog, boolean isShow);
	}

	public abstract String getMethod();

	protected void getListData(String ticket, final int pageNo, Which which) {
		String wsdl_url = SelfApplication.WSDL_URL;// wsdl
		String namespace = SelfApplication.WSDL_NAME_SPACE;// namespace
		String methodName = getMethod();// 要调用的方法名称
		SoapObject request = new SoapObject(namespace, methodName);
		request.addProperty("ticket", ticket);
		if (which.ordinal() == Which.PAGE.ordinal()) {
			request.addProperty("pageSize", PAGE_SIZE);
			request.addProperty("pageNo", pageNo);
		} else if (which.ordinal() == Which.COUNT.ordinal()) {
			request.addProperty("count", PAGE_SIZE.toString());
		} else if (which.ordinal() == Which.EOMS_ALARM.ordinal()) {
			request.addProperty("pageSize", PAGE_SIZE);
			request.addProperty("pageNo", pageNo);
			request.addProperty("workOrderType", OrderType.EOMS_ALARM.index + "");
		} else if (which.ordinal() == Which.EOMS_POWER.ordinal()) {
			request.addProperty("pageSize", PAGE_SIZE);
			request.addProperty("pageNo", pageNo);
			request.addProperty("workOrderType", OrderType.EOMS_POWER.index + "");
		} else if (which.ordinal() == Which.CASE_TASK.ordinal()) {
			request.addProperty("pageSize", PAGE_SIZE);
			request.addProperty("pageNo", pageNo);
			request.addProperty("workOrderType", OrderType.CASE_TASK.index + "");
		} else if (which.ordinal() == Which.CASE_ALARM.ordinal()) {
			request.addProperty("pageSize", PAGE_SIZE);
			request.addProperty("pageNo", pageNo);
			request.addProperty("workOrderType", OrderType.CASE_ALARM.index + "");
		}

		BuWebService webService = new BuWebService(wsdl_url, request);
		webService.setDoNet(false);
		webService.requestWSDL(new IWebService() {
			@Override
			public void logic(SoapSerializationEnvelope envelope) throws IOException, XmlPullParserException {
				SoapObject response = (SoapObject) envelope.getResponse();
				paser(pageNo, response);
			}
		});
	}

	protected abstract void paser(int pageNo, SoapObject response);
}
